/**
 * 283. 移动零
 * https://leetcode-cn.com/problems/move-zeroes/
 */
const { displayBaseArray } = require('../../libs/public');

/**
 * @param {any[]} arr
 * @param {number} a
 * @param {number} b
 */
function swap(arr, a, b) {
  const temp = arr[a];
  arr[a] = arr[b];
  arr[b] = temp;
}

/**
 * @param {number[]} nums
 * @return {void} Do not return anything, modify nums in-place instead.
 */
function moveZeroes(nums) {
  const lastIndex = nums.length - 1;
  let i = nums.indexOf(0);
  if (i < 0) {
    return;
  }
  let j = i + 1;
  while (j <= lastIndex) {
    if (nums[j] === 0) {
      j += 1;
      continue;
    }
    swap(nums, i, j);
    i += 1;
    j += 1;
  }
}

let nums;

nums = [0, 1, 0, 3, 12];
moveZeroes(nums);
console.log(displayBaseArray(nums) === '[1,3,12,0,0]');

nums = [0];
moveZeroes(nums);
console.log(displayBaseArray(nums) === '[0]');
